import Interface
import data

import requests
import tkinter as tk
import random

getUrl = data.baseUrl + '/genshin/get'

def display():
    # 生成第1组容器
    frame1 = tk.Frame(Interface.app)
    frame1.pack(side='left')
    tk.Label(frame1, text="输入要查询的词汇：", font=Interface.font).pack(side='left')

    # 生成第2组容器
    frame2 = tk.Frame(Interface.app)
    frame2.pack(side='left')
    global text
    text = tk.Entry(frame2, font=Interface.font)
    text.pack(side='left')
    text.bind('<FocusIn>', lambda event: text.delete('0', 'end'))
    btn = tk.Button(frame2, text="查询", command=lambda: query(text.get()), font=Interface.font).pack(side='left')
    Interface.app.bind("<Return>", lambda event: query(text.get()))  # 按钮绑定回车键

    # 创建画布和滚动条，Canvas,Scrollbar放置在主窗口上
    global canvas
    canvas = tk.Canvas(master=Interface.app)
    canvas.pack(side='left', fill='both', expand=True)
    canvas.bind_all("<MouseWheel>", lambda event: canvas.yview_scroll(-1 * (int(event.delta / 120)), "units"))
    global scro
    scro = tk.Scrollbar(master=Interface.app)
    scro.pack(side='right', fill='y')


def query(word):
    #获取数据
    url = getUrl + "?word=" + word
    headers = {"Accept": "application/vnd.github.v3+json"}
    data = requests.get(url, headers=headers)
    response_dict = data.json()

    # 定义frame列表
    frame = []
    i = 0
    n = 0
    words_list = []
    # 确保画布内容为空
    canvas.delete(tk.ALL)

    for item in response_dict["data"]["records"]:
        # 生成随机数，如果随机数和上一轮重复，则另外生成
        m = n
        while m == n:
            n = random.randint(0, len(Interface.color) - 1)
        # 生成第i组容器
        frame.append(tk.Frame(canvas))
        frame[i].pack(side='top', anchor="w")
        # 将Frame添加至Canvas上
        canvas.create_window(0, 5 * 30 * i + 100, window=frame[i], anchor="w")

        if item.get("zhCN"):
            tk.Label(frame[i], text="中文：" + item.get("zhCN"), fg=Interface.color[n], font=Interface.font).pack(side='top', anchor="w")
        if item.get("en"):
            tk.Label(frame[i], text="英文：" + item.get("en"), fg=Interface.color[n], font=Interface.font).pack(side='top', anchor="w")
        if item.get("ja"):
            tk.Label(frame[i], text="日语：" + item.get("ja"), fg=Interface.color[n], font=Interface.font).pack(side='top', anchor="w")
        if item.get("tags"):
            tk.Label(frame[i], text="标签：" + item.get("tags").replace(",", "  "), fg=Interface.color[n], font=Interface.font).pack(side='top', anchor="w")

        words_list.append(item)
        # 更新Frame大小，不然没有滚动效果
        frame[i].update()
        i += 1

    # 没有符合条件的结果
    if i == 0:
        # 生成容器
        frame.append(tk.Frame(canvas))
        frame[i].pack(side='left', anchor="w", fill="both")
        # 将Frame添加至Canvas上
        canvas.create_window(0, 5 * 30 * i + 100, window=frame[i], anchor="w")
        tk.Label(frame[i], text="未查询到结果", fg=Interface.color[n], font=Interface.font).pack(side='left', anchor="w", fill="both")

    # 将滚动按钮绑定在Canvas上
    canvas.configure(yscrollcommand=scro.set, scrollregion=canvas.bbox("all"))
    scro.config(command=canvas.yview)